//
//  BTConsultVC.m
//  BaxTer
//
//  Created by wsj2015 on 2016/11/17.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "BTConsultVC.h"

#import "BTCosultModel.h"
#import "BTConsultCell.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "BTBaseWebVC.h"
#import "CWQ_historySearchVC.h"
@interface BTConsultVC ()

@end

@implementation BTConsultVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)configUI{
  
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 -49);
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"BTConsultCell" bundle:nil] forCellReuseIdentifier:@"BTConsultCell"];
    UIBarButtonItem *right2 =[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"搜索_icon.png"] style:0 target:self action:@selector(toSearch)];
    
    self.navigationItem.rightBarButtonItems =@[right2];
    self.title  =@"资讯";
    weakSelf_define;
    [self.tableView wsj_HeaderRefreshBlock:^{
        [weakSelf getConsultList:YES];
    }];
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        [weakSelf getConsultList:NO];
    }];
    [self.tableView.header beginRefreshing];
    
}

- (void)toSearch{
    CWQ_historySearchVC *vc =[CWQ_historySearchVC new];
    vc.searchType = CWQ_historySearchVCType_Consult;
    [self.navigationController pushViewController:vc animated:YES];
    
   
}
- (void)getConsultList:(BOOL)isRecover{
  
    static NSInteger  page =1;
   
    if (isRecover) {
        page = 1;
    }else{
        page ++;
        
    }
    weakSelf_define;
    self.tableView.loading = YES;
    [[BTNetworkManager sharedMg] POST:kConsultsList parameters:nil success:^(id responseObject) {
         weakSelf.tableView.loading = NO;
        [weakSelf.tableView.footer endRefreshing];
        [weakSelf.tableView.header endRefreshing];
 
            if ([responseObject[@"data"] isKindOfClass:[NSArray class]]) {
                NSInteger totalPage;
                
                if ([responseObject[@"totalPage"] isKindOfClass:[NSNull class]] ) {
                    totalPage = 0;
                }else{
                    totalPage = [responseObject[@"totalPage"] integerValue];;
                }
                NSInteger currentPage = [responseObject[@"page"] integerValue];
                if (currentPage >=totalPage) {
                    [weakSelf.tableView.footer noticeNoMoreData];
                    
                }else{
                    [weakSelf.tableView.footer resetNoMoreData];
                    
                }
                if (isRecover) {
                    [weakSelf.dataArray removeAllObjects];
                    
                }else{
                    
                }
                [weakSelf.dataArray addObjectsFromArray:[BTCosultModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]]];
                
                [weakSelf.tableView reloadData];
            }
        
        
    } failure:^(NSString *err) {
        weakSelf.tableView.loading = NO;
        [weakSelf.tableView.footer endRefreshing];
        [weakSelf.tableView.header endRefreshing];
    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
 
    return   [tableView fd_heightForCellWithIdentifier:@"BTConsultCell" configuration:^(BTConsultCell * cell) {
             BTCosultModel *model = self.dataArray[indexPath.row];
             cell.model = model;
    }];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BTConsultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BTConsultCell" forIndexPath:indexPath];
    BTCosultModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    BTCosultModel *model = self.dataArray[indexPath.row];
    
    BTBaseWebVC *vc =[BTBaseWebVC new];
    NSString *typeStr = nil;
    NSString *url;
    if ([model.type isEqualToString:@"ORDINARY"]) {
        typeStr = kConsultDetail;
        url = [NSString stringWithFormat:@"%@/%@/%@",[BTNetworkManager sharedMg].bathURL,typeStr,model.id];
    }else if([model.type isEqualToString:@"QUESTION"]){
        typeStr = kQuestionDetai;
        url = [NSString stringWithFormat:@"%@/%@/%@",[BTNetworkManager sharedMg].bathURL,typeStr,model.questionID];
    }
    vc.tagKey  =  @"consult";
    vc.url =url;
    [self.navigationController pushViewController:vc animated:YES];
    

}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
